package implementation;

import protocol.Query;
import protocol.State;
import protocol.StateObject;

public class NumTixHandler {
	public static void handleNumTix(StateObject nextState, Query query) {
		String standardPrompt = "HowManyTickets";
		String helpPrompt = "NumTixHelp";
		ExecUtils.playPrompt(standardPrompt, nextState);
		boolean receivedInput = false;

		while (!receivedInput) {
			ExecUtils.getUserInput(query, "gram_numtix");

			if (query.getShortConf() != null) {
				query.voidTransaction();
				String response = query.getShortConf().trim().toLowerCase();
				if (response.equals("assistance")) {
					ExecUtils.playPrompt(helpPrompt, nextState);
				} 
				else if (response.equals("goback") || response.equals("go back") || 
						response.equals("startover") || response.equals("start over")) {
					ExecUtils.playPrompt(response, nextState);
					receivedInput = true;
					nextState.setState(State.NUMTIX);
				}
			} else if ( !query.numTixIsNew() ) {
				if (query.homeTeamIsNew() || query.awayTeamIsNew() || query.dateIsNew() || query.priceIsNew()) {
					// explicit confirmation for complex query
					boolean success = ExecUtils.explicitConfirm(query, nextState);
					if (!success)
						query.voidTransaction();
					ExecUtils.playPrompt(standardPrompt, nextState);
				} else {
					query.voidTransaction();
					ExecUtils.playPrompt(helpPrompt, nextState);
				}
			} else {
				receivedInput = true;

				if (query.homeTeamIsNew() || query.awayTeamIsNew() 
						|| query.dateIsNew() || query.priceIsNew())
				{
					// Complex query
					boolean success = ExecUtils.explicitConfirm(query, nextState);
					if (!success)
					{
						query.voidTransaction();
						receivedInput = false;
						ExecUtils.playPrompt(standardPrompt, nextState);
					}
				}

				if(receivedInput){
					if (query.getHomeTeam() == null)
						nextState.setState(State.HOMETEAM);
					else if (query.getAwayTeam() == null)
						nextState.setState(State.AWAYTEAM);
					else
						nextState.setState(State.AWAYTEAMCONF);
				}
			}
		}
	}
}
